package com.zzgc.modules.service.impl;

import com.zzgc.common.dao.SchoolPlanDao;
import com.zzgc.common.entity.SchoolPlan;
import com.zzgc.modules.service.SchoolPlanService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (SchoolPlan)表服务实现类
 *
 * @author makejava
 * @since 2023-05-10 16:00:13
 */
@Service("schoolPlanService")
public class SchoolPlanServiceImpl implements SchoolPlanService {
    @Resource
    private SchoolPlanDao schoolPlanDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SchoolPlan queryById(Long id) {
        return this.schoolPlanDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param schoolPlan 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SchoolPlan> queryByPage(SchoolPlan schoolPlan) {
        List<SchoolPlan> schoolPlans = this.schoolPlanDao.queryByLimit(schoolPlan);
        long total = this.schoolPlanDao.count(schoolPlan);
        if (schoolPlans.size() == 0) {
            return new ArrayList<>();
        }
        schoolPlans.get(0).setTotal(total);
        return schoolPlans;
    }

    /**
     * 统计学校学生数量
     *
     * @param schoolPlan 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SchoolPlan> selectStudentNumber(SchoolPlan schoolPlan) {
        return this.schoolPlanDao.selectStudentNumber(schoolPlan);
    }

    /**
     * 新增数据
     *
     * @param schoolPlan 实例对象
     * @return 实例对象
     */
    @Override
    public SchoolPlan insert(SchoolPlan schoolPlan) {
        this.schoolPlanDao.insert(schoolPlan);
        return schoolPlan;
    }

    /**
     * 修改数据
     *
     * @param schoolPlan 实例对象
     * @return 实例对象
     */
    @Override
    public SchoolPlan update(SchoolPlan schoolPlan) {
        this.schoolPlanDao.update(schoolPlan);
        return this.queryById(schoolPlan.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.schoolPlanDao.deleteById(id) > 0;
    }
}
